package com.zmzncs.lmtc.common.interactive.mp;

import com.alibaba.fastjson.JSONObject;
import com.zmzncs.lmtc.common.pojo.mpMessage.GiveTicketMessage;
import com.zmzncs.lmtc.common.pojo.mpMessage.RefundFalse;
import com.zmzncs.lmtc.common.pojo.mpMessage.RefundSuccess;
import com.zmzncs.lmtc.common.util.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * @Author 高博文
 * @Date 2020/10/26 10:41
 */
@Slf4j
@Component
public class MpMessage {

    //退款成功通知
    private String refundSuccess;
    @Value("${mp.message-url.serviceProgress}")
    public void setRefundSuccess(String refundSuccess) {
        this.refundSuccess = refundSuccess;
    }

    //退款失败通知
    private String refundFalse;
    @Value("${mp.message-url.refundFalse}")
    public void setRefundFalse(String refundFalse) {
        this.refundFalse = refundFalse;
    }

    //优惠券发放通知
    private String giveTicket;
    @Value("${mp.message-url.giveTicket}")
    public void setGiveTicket(String giveTicket) {
        this.giveTicket = giveTicket;
    }

    /**
     * 退款成功通知
     */
    @Async("threadPoolExecutor")
    public void refundSuccessMessage(RefundSuccess message){
        log.info("线程池：{}", Thread.currentThread().getName());
        try {
            String unionid = message.getUnionid();
            if(unionid != null){
                log.info("退款成功消息，unionid:【{}】", unionid);
                String jsonStr = JSONObject.toJSONString(message);
                String isSuccess = HttpUtil.requestPostBody(refundSuccess, jsonStr, null);
                if (isSuccess != null) {
                    log.info( "退款成功公众号消息发送成功");
                }
            } else {
                log.info("退款成功消息发送-客户【{}】未在线", unionid);
            }
        } catch (Exception e) {
            log.error("退款成功发送消息失败【{}】", e.getMessage());
        }
    }

    /**
     * 拒绝退款通知
     */
    @Async("threadPoolExecutor")
    public void refundFalseMessage(RefundFalse message){
        log.info("线程池：{}", Thread.currentThread().getName());
        try {
            String unionid = message.getUnionid();
            if(unionid != null){
                log.info("拒绝退款消息，unionid:【{}】", unionid);
                String jsonStr = JSONObject.toJSONString(message);
                String isSuccess = HttpUtil.requestPostBody(refundFalse, jsonStr, null);
                if (isSuccess != null) {
                    log.info( "拒绝退款公众号消息发送成功");
                }
            } else {
                log.info("拒绝退款消息发送-客户【{}】未在线", unionid);
            }
        } catch (Exception e) {
            log.error("拒绝退款发送消息失败【{}】", e.getMessage());
        }
    }

    /**
     * 优惠券发放通知
     */
    @Async("threadPoolExecutor")
    public void giveTicketMessage(GiveTicketMessage message){
        log.info("线程池：{}", Thread.currentThread().getName());
        try {
            String unionid = message.getUnionid();
            if(unionid != null){
                log.info("优惠券发放消息，unionid:【{}】", unionid);
                String jsonStr = JSONObject.toJSONString(message);
                String isSuccess = HttpUtil.requestPostBody(giveTicket, jsonStr, null);
                if (isSuccess != null) {
                    log.info( "优惠券发放公众号消息发送成功");
                }
            } else {
                log.info("优惠券发放消息发送-客户【{}】未在线", unionid);
            }
        } catch (Exception e) {
            log.error("优惠券发放发送消息失败【{}】", e.getMessage());
        }
    }



}
